<?php defined('SYSPATH') or die('No direct script access.');

require_once Kohana::find_file('classes', 'util');

// URL: /<context>/index.php/Tasl/
class Controller_Task extends Controller_Base 
{
	private $categoryService;
	private $taskService;
	
	public function __construct(Request $request, Response $response)
	{
		parent::__construct($request, $response);
		
		$this->categoryService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_CATEGORY.'/', ACCESS_TOKEN);
		$this->categoryService->init($this->_requestContext);

		$this->taskService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_TASK.'/', ACCESS_TOKEN);
		$this->taskService->init($this->_requestContext);
	}
	
	
	public function action_add()
	{
		if ($this->request->method() == "GET") {
			$this->showForm(null);
		} else if ($this->request->method() == "POST") {
			$this->doAdd();
		}
	}
	
	public function action_edit()
	{
		if ($this->request->method() == "GET") {
			$sid = $this->request->query('sid');
			$this->showForm($sid);
		} else if ($this->request->method() == "POST") {
			$this->doUpdate();
		}
	}
	
	private function showForm($sid)
	{
		$title = "Seednia::New Project";
		$task = null;
		if ($sid != null) {
			$task = $this->taskService->getByXid($sid);
			$title = "Seednia::Task ".$task->name;
		} else {
			$this->template->set_global("enableProjectNavBar", false);
		}
		$contentFrame = $this->loadViews($title, '_default_', 'default_sbar_right');		
		$content = $this->createView('task_form');
		$contentFrame->content = $content;
		
		$content->tasks = $tasks;
		
		
		$cateCriteria = array('kind' => 'Task');
		$ref_categories = $this->categoryService->getList($cateCriteria, 0, 10);
		$content->ref_categories = Util_Data::toMapptingArray($ref_categories, 'sid', 'name');
			
		$this->_projectHelper->loadUserProjects($this->template);
	}
	
	private function doAdd()
	{
		try {
			$redirTo = CONTROLLERPAGE.'/Project';
			
			$record = $this->loadFormParams();
			$sid = $this->taskService->add($record);
			
			if ($sid != null) {
				; // TODO: handle error
			}

			$this->request->redirect($redirTo);
		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Login | Message: ' . $ex->getMessage());
		}
	}
	
	private function doUpdate()
	{
		try {
			$redirTo = CONTROLLERPAGE.'/Project';
			
			$record = $this->loadFormParams();
			$this->taskService->update($record);

			$this->request->redirect($redirTo);
		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Login | Message: ' . $ex->getMessage());
		}
	}
	
	private function loadFormParams()
	{
		require_once Kohana::find_file('classes', 'util');
		$record = array();
		
		//$projectSid = (int)$this->request->query('projectSid');
		$projectSid = $this->_sessionInfo->currentProjectSid;
		$record['projectSid'] = $projectSid;
		$record['name'] = $this->request->post('name');
		$record['description'] = $this->request->post('description');
		$record['status'] = (int)$this->request->post('status');
		$record['baselineDate'] = datetime_format_convert($this->request->post('planStartDate'),  $this->_sessionInfo->userInfo['dateFormat'], FORMAT_DATE_ISO);
		$record['actualDate'] = datetime_format_convert($this->request->post('actualDate'),  $this->_sessionInfo->userInfo['dateFormat'], FORMAT_DATE_ISO);
		
		$record['privacyLevel'] = (int)$this->request->post('privacyLevel');

		return $record;
	}
	

} // End Controller_Home